#include "AI.h"

extern list<WordInfo> word_list;
extern list<WordInfo>::iterator word_it;

extern list<AdditionalInfo> additional_list;
extern list<AdditionalInfo>::iterator additional_it;

extern map<string,int> number_map;
extern map<string,int>::iterator number_it;

extern list<string> num_alpha_list;
extern list<string>::iterator num_alpha_it;

void MeasureTag()
{
	unsigned int n;
	string s1;
	string s2;

	bool intFlag;

	for(word_it = word_list.begin(); word_it != word_list.end(); word_it++)
	{
		s1.clear();
		s2.clear();

		intFlag = false;
		n = 0;

		if(word_it->type == "N/A")
		{
			while(n < word_it->word.length())
			{
				if(!intFlag)
				{
					if((word_it->word[n] < '0' || word_it->word[n] > '9') && word_it->word[n] != '.')
					{
						intFlag = true;
						s2.push_back(word_it->word[n]);
					}

					else
					{
						s1.push_back(word_it->word[n]);
					}
				}

				else
				{
					s2.push_back(word_it->word[n]);
				}

				n++;
			}

			AdditionalInfo tmp_AdditionalInfo;
			bool numberFlag = true;

			if(s1.empty()) // s1 == ""
			{
				list<WordInfo>::iterator word_it_2 = word_it;

				if(word_it_2 != word_list.begin())
				{
					word_it_2--;

					if(word_it_2->type == "FLOAT" || word_it_2->type == "INT" || word_it_2->type == "INT1" || word_it_2->type == "INT2" || word_it_2->type == "INT3" || word_it_2->type == "INT4")
					{
						s1 = word_it_2->word;
						numberFlag = true;
					}

					else if(word_it_2->type == "NUM_ALPHA")
					{
						num_alpha_list.push_front(word_it_2->word);

						while(word_it_2 != word_list.begin())
						{
							word_it_2--;

							if(word_it_2->type == "NUM_ALPHA")
							{
								num_alpha_list.push_front(word_it_2->word);
							}

							else
							{
								break;
							}
						}

						std::ostringstream oss;
						oss << EnglishNum();

						s1 = oss.str();

						numberFlag = true;

					}

					else
					{
						numberFlag = false;
					}
				}

				else
				{
					numberFlag = false;
				}
			}

			if(numberFlag)
			{
				if(s2 == "kg")
				{
					tmp_AdditionalInfo.word = s1 + "kg";
					tmp_AdditionalInfo.type = "WEIGHT_N";
					additional_list.push_back(tmp_AdditionalInfo);
				}

				else if(s2 == "g")
				{
					double d = atof(s1.c_str()) / 1000.0f;
					std::ostringstream oss;
					oss << d;
					tmp_AdditionalInfo.word = oss.str() + "kg";

					tmp_AdditionalInfo.type = "WEIGHT_N";
					additional_list.push_back(tmp_AdditionalInfo);
				}

				else if(s2 == "cm")
				{
					double d = atof(s1.c_str()) / 100.0f;
					std::ostringstream oss;
					oss << d;
					tmp_AdditionalInfo.word = oss.str() + "m";

					tmp_AdditionalInfo.type = "LENGTH_N";
					additional_list.push_back(tmp_AdditionalInfo);
				}

				else if(s2 == "m")
				{
					tmp_AdditionalInfo.word = s1 + "m";
					tmp_AdditionalInfo.type = "LENGTH_N";
					additional_list.push_back(tmp_AdditionalInfo);
				}
			}
		}
	}
}